<?php

/**
 * Project: Vietsam
 * Package Controller
 * Class: Tab
 * Author: Khoi Pham		email: pmkhoi@gmail.com
 * Created on Dec 18, 2008
 * Updated on Dec 30, 2008
 *  
 * */
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Profile extends MY_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('profile_model', 'profile', true);
        $this->load->helper('form');
        $this->main = '';
        $this->checkLogin();
        $this->checkPermission();

        $this->load->model('profile_model', 'profile', true);
        $this->template->set_template('admin');
        $this->buildMenu();
    }

    function Profile() {
        parent::VNT_Controller();
        $this->load->library('xajax', 'profile');
        $this->xajax->registerFunction(array('test', &$this, 'test'));

        $this->xajax->processRequest();
        $this->xajax_js = $this->xajax->getJavascript($this->base_url . 'js/');
        $this->load->model('profile_model', 'profile', true);
        $this->load->helper('form');
        $this->main = '';
        $this->checkLogin();
        $this->checkPermission();
    }

    function index() {
        $this->view();
    }

    function create() {
        $data = array();
        $data['id'] = 0;
        $data['vi_name'] = '';
        $data['en_name'] = '';
        $data['error_message'] = '';
        $data['legend_text'] = lang('create_profile');

        $this->template->parse_view('content', 'admin/profile/create_profile', $data);
        $this->template->render();

        //$this->main = $this->load->view('admin/profile/create_profile', $data, true);
        //$this->execute();
    }

    function edit($id_profile) {
        $profile = $this->profile->getById($id_profile);
        if (!$profile) {
            redirect('admin/profile');
            return;
        }

        $data = array();
        $data['id'] = $profile->id_profile;
        $data['vi_name'] = $profile->vi_name;
        $data['en_name'] = $profile->en_name;
        $data['error_message'] = '';
        $data['legend_text'] = lang('edit_profile');

         $this->template->parse_view('content', 'admin/profile/create_profile', $data);
        $this->template->render();
        
    }

    function save() {
        $id = (int) $this->input->post('txtID');
        $profile = new stdClass();
        if ($id > 0) {
            $profile = $this->profile->getById($id);
            if (!$profile) {
                redirect('admin/profile');
                return;
            }
        } else {
            $profile->id_profile = 0;
        }

        $profile->vi_name = trim($this->input->post('txtViName'));
        $profile->en_name = trim($this->input->post('txtEnName'));

        $error = false;
        $error_message = '';

        $this->load->library('form_validation');

        $this->form_validation->set_rules('txtViName', 'lang:vi_name', 'required|min_length[3]|max_length[50]');
        $this->form_validation->set_rules('txtEnName', 'lang:en_name', 'required|min_length[3]|max_length[50]');

        if ($this->form_validation->run() == FALSE) {
            $data = array();
            $data['id'] = $profile->id_profile;
            $data['vi_name'] = $profile->vi_name;
            $data['en_name'] = $profile->en_name;

            $data['error_message'] = '<div style="color:red">' . validation_errors() . '</div>';
            $data['legend_text'] = $id > 0 ? lang('edit_profile') : lang('create_profile');
            $this->main = $this->load->view('admin/profile/create_profile', $data, true);
            $this->execute();
        } else {
            if ($id > 0) {
                $result = $this->profile->update($profile);
            } else {

                $this->profile->vi_name = $profile->vi_name;
                $this->profile->en_name = $profile->en_name;

                $id_profile = $this->profile->insert();

                if ($id_profile > 0) {
                    //Get all tab:
                    $this->load->model('tab_model', 'tab', true);
                    $tabs = $this->tab->getAll(-1);
                    $id_tabs = array();
                    for ($i = 0; $i < count($tabs); $i++) {
                        $id_tabs[] = $tabs[$i]->id_tab;
                    }

                    $this->load->model('access_model', 'access', true);
                    $this->access->insert($id_profile, $id_tabs);
                }
            }

            redirect('admin/profile/');
        }
    }

    function view() {
        $NUMBER_RECORD_PER_PAGE = 15;
        $this->load->library('pagination');
        $parent = $this->uri->segment(4, 0);
        $page = (int) $this->uri->segment(5);
        $config['base_url'] = $this->base_url . 'admin/profile/view/' . $parent;
        $config['total_rows'] = $this->profile->getAllCount();
        $config['per_page'] = $NUMBER_RECORD_PER_PAGE;
        $config['uri_segment'] = 5;
        $config['first_link'] = 'First';

        $this->pagination->initialize($config);

        $rows = $this->profile->getAll($page, $NUMBER_RECORD_PER_PAGE);
        $data = array();
        $data['rows'] = $rows;

        $this->template->parse_view('content', 'admin/profile/profile_list', $data);
        $this->template->render();

        //$this->main = $this->load->view('admin/profile/profile_list', $data, true);

        //$this->execute();
    }

    function delete() {
        $id = intval($this->uri->segment(4));
        $ids = array();
        if ($id > 0) {
            $ids[] = (int) $id;
            //$this->product->delete($id);
        } else {
            if (($this->input->post('submitDelcategory'))) {
                $ids = $this->input->post('categoryBox');
            }
        }

        if (in_array(1, $ids)) {
            redirect('admin/profile/');
            return;
        }

        if (count($ids)) {

            $result = $this->profile->delete($ids);
        }

        redirect('admin/profile/');
    }

}

?>
